﻿Public Class Admin
    Inherits System.Web.UI.MasterPage

    Dim DBManager As New DBManager
    Private userName As String
    Private password As String
    Private userLoggedin As userAcc

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Check if user is null
        If (Session("user") IsNot Nothing) Then
            'Retrieve userAcc details from session
            Dim userLoggedIn As userAcc = Session("user")
            'Check if user is admin, assign masterpage respectively
            lblWelcome.Visible = True
            lbProfile.Visible = True
            lbProfile.Text = userLoggedIn.display_name
            lbProfile.PostBackUrl = "~/Profile.aspx?id=" + userLoggedIn.display_name
            btnLoginModal.Visible = False
            btnLogoutModal.Visible = True
        Else
            btnLoginModal.Visible = True
            btnLogoutModal.Visible = False
            lblWelcome.Visible = False
            lbProfile.Visible = False
        End If

        If (IsPostBack) Then
            hfModalVisible.Value = "hidden"
        End If

    End Sub

    'Login/register functions
    Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As EventArgs)

        'Ensure no empty text
        If (tbxEmail.Text.Trim.Count > 0 And tbxPassword.Text.Trim.Count > 0) Then
            'Check if login successful
            'Username not case sensitive
            If (DBManager.login(tbxEmail.Text.ToLower().ToString(), tbxPassword.Text.ToString())) Then

                'Retrieve user details
                userLoggedin = DBManager.getUserDetails(tbxEmail.Text.ToString())
                'Assign userAcc to session variable
                Session("user") = userLoggedin
                Session("ID") = userLoggedin.userID
                hfModalVisible.Value = "hidden"
                ModalPopupExtenderLogin.Hide()
                Response.Redirect(Request.RawUrl)

            Else
                lblFailedLogin.Visible = True
                hfModalVisible.Value = "shown"
            End If
        End If
    End Sub

    Protected Sub lbProfile_Click(ByVal sender As Object, ByVal e As EventArgs)
        Try
            Response.Redirect("Profile.aspx?id=" + userLoggedin.display_name)
        Catch ex As Exception
        End Try

    End Sub

    Protected Sub btnRegisterAcc_Click(ByVal sender As Object, ByVal e As EventArgs)
        tbxEmail.Text = ""
        tbxPassword.Text = ""
        hfModalVisible.Value = "hidden"
        ModalPopupExtenderLogin.Hide()
        lblFailedLogin.Visible = False
        Response.Redirect("Register.aspx")
    End Sub

    Protected Sub btnCloseLogin_Click(ByVal sender As Object, ByVal e As EventArgs)
        tbxEmail.Text = ""
        tbxPassword.Text = ""
        ModalPopupExtenderLogin.Hide()
        hfModalVisible.Value = "hidden"
        lblFailedLogin.Visible = False
    End Sub

    Protected Sub btnLoginModal_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnLoginModal.Click
        hfModalVisible.Value = "shown"
        lblFailedLogin.Visible = False
        ModalPopupExtenderLogin.Show()
    End Sub

    'Logout functions
    Protected Sub btnLogout_Click(ByVal sender As Object, ByVal e As EventArgs)
        'Clears the session
        Session.Abandon()
        Response.Redirect(Request.Path)
    End Sub
End Class